package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ActivitiConstants;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.AutoApplyMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.AutoApply;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.activiti.base.WorkFlowService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.AutoApplyService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.TaskLinkIamService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by yangsy on 2016/12/16.
 */

public class AutoApplyServiceImpl implements AutoApplyService {
    public static final String FLOWKEY1 = "carApply";
    public static final String FLOWKEY2 = "carDispatch";
    @Autowired
    private AutoApplyMapper autoApplyMapper;
    @Autowired
    private TaskLinkIamService taskLinkIamService;
    @Autowired
    private WorkFlowService workFlowService;

    @Override
    public int deleteByPrimaryKey(String autoApplyId) {
        return autoApplyMapper.deleteByPrimaryKey(autoApplyId);
    }

    @Override
    public int insert(AutoApply record) {
        return autoApplyMapper.insert(record);
    }

    /**
     * 发起用车申请流程
     * TODO: 2017/3/15,0015
     *
     * @param autoApply
     * @param employeeVo
     */
    @Override
    public int saveSelective(AutoApply autoApply, EmployeeVo employeeVo) throws Exception {
        int recount = 0;
        if (null != autoApply) {
            //保存数据
            recount += autoApplyMapper.insertSelective(autoApply);
            //开启流程
            Map<String, Object> variables = new HashMap<>();
            String[] str = {autoApply.getFirstApproval()};
            variables.put(ActivitiConstants.MANUAL_PERSON, new ArrayList<>(Arrays.asList(str)));
            workFlowService.startProcessInstanceByKey(FLOWKEY1, autoApply.getAutoApplyId(), variables, employeeVo.getEmployeeId());
        }
        return recount;
    }

    @Override
    public AutoApply selectByPrimaryKey(String autoApplyId) {
        return autoApplyMapper.selectByPrimaryKey(autoApplyId);
    }

    @Override
    public int updateByPrimaryKeySelective(AutoApply record) {
        return autoApplyMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AutoApply record) {
        return autoApplyMapper.updateByPrimaryKey(record);
    }

    @Override
    public JSONObject list(AutoApply autoApply) {
        JSONObject jsonObject = new JSONObject();
        autoApply.setLimit(autoApply.getLimit() == null ? 10 : autoApply.getLimit());
        autoApply.setOffset(autoApply.getOffset() == null ? 0 : autoApply.getOffset());
        jsonObject.put("total", autoApplyMapper.queryCount(autoApply));
        jsonObject.put("rows", JSON.toJSON(autoApplyMapper.queryList(autoApply)));
        return jsonObject;
    }

    /**
     * 代办查找
     *
     * @param bootstrapSearchBean
     * @return
     */
    @Override
    public List<Object> searchByPIId(BootstrapSearchBean bootstrapSearchBean) {
        return autoApplyMapper.searchByPIId(bootstrapSearchBean);
    }

    /**
     * 节点处理
     *
     * @param autoApply
     * @param employeeVo
     * @return
     */
    @Override
    public void dispose(AutoApply autoApply, EmployeeVo employeeVo) throws Exception {
        Map map = new HashMap<>();
        map.put("cond", autoApply.getStatus());
        if (autoApply.getStatus() == 1) {
            if (autoApply.getStepName().contains("申请单位领导审批")) {
                //设置下一步审批人
                String[] str = {autoApply.getSecondApproval()};
                map.put(ActivitiConstants.MANUAL_PERSON, new ArrayList<>(Arrays.asList(str)));
            } else if (autoApply.getStepName().contains("重新填写")) {
                //设置下一步审批人
                String[] str = {autoApply.getFirstApproval()};
                map.put(ActivitiConstants.MANUAL_PERSON, new ArrayList<>(Arrays.asList(str)));
                //修改用车单为审批中
                autoApply.setStatus(new Short("0"));
                autoApplyMapper.updateByPrimaryKeySelective(autoApply);
            } else if (autoApply.getStepName().contains("申请单位分管厂领导审批")) {
                //修改用车单状态
                autoApplyMapper.updateByPrimaryKeySelective(autoApply);
            }
        } else if (autoApply.getStatus() == 2) {
            if (!autoApply.getStepName().contains("重新填写")) {
                //驳回
                rejectTask(autoApply, map);
            } else {
                //废弃
                autoApply.setStatus(new Short("3"));
                autoApplyMapper.updateByPrimaryKeySelective(autoApply);
            }
        }
        workFlowService.completeTask(autoApply.getTaskId(), map, autoApply.getComments(), employeeVo.getEmployeeId());
    }

    /**
     * 修改任务状态，设置驳回到创建人
     */
    private int rejectTask(AutoApply autoApply, Map map) {
        //驳回到创建人
        map.put(ActivitiConstants.MANUAL_PERSON, new ArrayList<>(Arrays.asList(autoApply.getOperatorId())));
        autoApply.setStatus(new Short("2"));
        return autoApplyMapper.updateByPrimaryKeySelective(autoApply);
    }


    /**
     * 代办总条数
     *
     * @param bootstrapSearchBean
     * @return
     */
    @Override
    public int totalCountByPIId(BootstrapSearchBean bootstrapSearchBean) {
        return autoApplyMapper.totalCountByPIId(bootstrapSearchBean);
    }

    /**
     * 查询我已经审批的
     *
     * @param autoApply
     * @return
     */
    @Override
    public JSONObject processed(AutoApply autoApply) {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        autoApply.setLimit(autoApply.getLimit() == null ? Integer.valueOf(10) : autoApply.getLimit());
        autoApply.setOffset(autoApply.getOffset() == null ? Integer.valueOf(0) : autoApply.getOffset());

        // 总条数
        jsonObject.put("total", autoApplyMapper.queryProcessedCount(autoApply));
        List<AutoApply> trainList = autoApplyMapper.queryProcessedList(autoApply);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }


    /**
     * 查询我的出差待办
     *
     * @param autoApply
     */
    @Override
    public JSONObject listToDoTask(AutoApply autoApply) {
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        autoApply.setLimit(autoApply.getLimit() == null ? Integer.valueOf(10) : autoApply.getLimit());
        autoApply.setOffset(autoApply.getOffset() == null ? Integer.valueOf(0) : autoApply.getOffset());
        // 总条数
        jsonObject.put("total", autoApplyMapper.queryToDoCount(autoApply));
        List<AutoApply> trainList = autoApplyMapper.queryToDoList(autoApply);
        jsonObject.put("rows", JSON.toJSON(trainList));
        return jsonObject;
    }

    /**
     * 查询已完成的申请单
     */
    @Override
    public JSONObject queryFinishApply(AutoApply autoApply) {
        JSONObject jsonObject = new JSONObject();
        autoApply.setLimit(autoApply.getLimit() == null ? 10 : autoApply.getLimit());
        autoApply.setOffset(autoApply.getOffset() == null ? 0 : autoApply.getOffset());
        autoApply.setStatus(new Short("1"));
        jsonObject.put("total", autoApplyMapper.queryCount(autoApply));
        jsonObject.put("rows", JSON.toJSON(autoApplyMapper.queryList(autoApply)));
        return jsonObject;
    }
}
